<?php

namespace App\Console\Commands;

use App\Models\ChallengeGame;
use App\Models\Championship;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdateNewsletter extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'batch:newsletter';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成乒乓头条';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $championshipCount = 2;
        $challengeCount = 20;

        $lastMonthDay = Carbon::now()->subMonth();
        $lastTwoDay = Carbon::now()->subDays(2);

        //查询全部锦标赛
        $championship = Championship::where([
            ['status', '>', 10],
            ['game_datetime', '>', $lastMonthDay],
        ])
            ->select('id', 'city', 'title', 'game_datetime', 'signup_datetime', 'address', 'remark', 'status', 'view')
            ->orderBy('view', 'desc')
            ->get();

        //查询全部挑战赛
        $challengeGames = ChallengeGame::join('wechat_users as usera', function ($join) {
            $join->on('challenge_games.play_a', '=', 'usera.id');
        })
            ->join('wechat_users as userb', function ($join) {
                $join->on('challenge_games.play_b', '=', 'userb.id');
            })
            ->where('challenge_games.game_datetime', '>', $lastTwoDay)
            ->select(
                'challenge_games.id',
                'challenge_games.city',
                'usera.name as usera_name',
                'usera.nickname as usera_nickname',
                'usera.head_img as usera_head_img',
                'userb.name as userb_name',
                'userb.nickname as userb_nickname',
                'userb.head_img as userb_head_img',
                'challenge_games.game_datetime',
                'challenge_games.type',
                'challenge_games.status',
                'challenge_games.view'
            )
            ->orderBy('challenge_games.view', 'desc')
            ->get();

        $list = [];
        $countList = array();

        foreach ($championship as $vo) {
            $ret = $this->checkCount($countList, $vo->city, $championshipCount);
            $countList = $ret['countList'];
            if ($ret['status']) {
                continue;
            }

            $data = $vo->tojson();
            $new = ['city' => $vo->city, 'type' => 1, 'context' => $data];
            $list[] = $new;
        }

        $countList = array();

        foreach ($challengeGames as $vo) {
            $ret = $this->checkCount($countList, $vo->city, $challengeCount);
            $countList = $ret['countList'];
            if ($ret['status']) {
                continue;
            }

            $data = $vo->tojson();
            $new = ['city' => $vo->city, 'type' => 2, 'context' => $data];
            $list[] = $new;
        }

        if (count($list) > 0) {
            //删除旧资料
            \DB::table('newsletters')->truncate();
            \DB::table('newsletters')->insert($list);
        }
        $this->info('执行成功');

    }

    private function checkCount($countList, $city, $n)
    {
        $status = false;
        if (array_key_exists($city, $countList)) {
            if ($countList[$city] >= $n) {
                $status = true;
            } else {
                $countList[$city]++;
            }
        } else {
            $countList[$city] = 1;
        }

        return ['countList' => $countList, 'status' => $status];
    }
}
